{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Houses in a Circular Street"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #interactive"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #交互"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: houseCount"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #计算一个环形街道上的房屋数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个代表环形街道的类 <code>Street</code> 和一个正整数 <code>k</code>，表示街道上房屋的最大数量（也就是说房屋数量不超过 <code>k</code>&nbsp;）。每个房屋的门初始时可以是开着的也可以是关着的。</p>\n",
    "\n",
    "<p>刚开始，你站在一座房子的门前。你的任务是计算街道上的房屋数量。</p>\n",
    "\n",
    "<p><code>Street</code> 类包含以下函数：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>void openDoor()</code> ：打开当前房屋的门。</li>\n",
    "\t<li><code>void closeDoor()</code> ：关闭当前房屋的门。</li>\n",
    "\t<li><code>boolean isDoorOpen()</code> ：如果当前房屋的门是开着的返回 <code>true</code> ，否则返回 <code>false</code> 。</li>\n",
    "\t<li><code>void moveRight()</code> ：向右移动到下一座房屋。</li>\n",
    "\t<li><code>void moveLeft()</code> ：向左移动到上一座房屋。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回 <code>ans</code>，它表示街道上的房屋数量。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>street = [0,0,0,0], k = 10\n",
    "<b>输出：</b>4\n",
    "<b>解释：</b>街道上有 4 座房屋，它们的门都是关着的。\n",
    "房屋数量小于 k，即 10。</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>street = [1,0,1,1,0], k = 5\n",
    "<b>输出：</b>5\n",
    "<b>解释：</b>街道上有 5 座房屋，向右移动时第 1、3 和 4 座房屋的门是开着的，其余的门都是关着的。房屋数量等于 k，即 5。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>解释：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n&nbsp; 是房屋数量</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= k &lt;= 10<sup>3</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-houses-in-a-circular-street](https://leetcode.cn/problems/count-houses-in-a-circular-street/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-houses-in-a-circular-street](https://leetcode.cn/problems/count-houses-in-a-circular-street/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,0,0,0]\\n10', '[1,0,1,1,0]\\n5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import Optional\n",
    "\n",
    "class Solution:\n",
    "    def houseCount(self, s: Optional['Street'], k: int) -> int:\n",
    "        # Step 1: Move to an open door\n",
    "        while not s.isDoorOpen():\n",
    "            s.moveRight()\n",
    "        \n",
    "        # Step 2 & 3: Move to the right and count, close any open door and record its index\n",
    "        for i in range(1, k + 1):\n",
    "            s.moveRight()\n",
    "            if s.isDoorOpen():\n",
    "                ans = i\n",
    "                s.closeDoor()  # Step 3: Close the door\n",
    "                \n",
    "        # Step 4: Return the last index of an open door encountered\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        i = 0\n",
    "        while i < k and not street.isDoorOpen():\n",
    "            i += 1\n",
    "            street.moveRight()\n",
    "        street.moveRight()\n",
    "        i = 0\n",
    "        while i < k:\n",
    "            if street.isDoorOpen():\n",
    "                r = i \n",
    "                street.closeDoor()\n",
    "            street.moveRight()\n",
    "            i += 1\n",
    "        return r + 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen(): street.moveRight()\n",
    "        street.moveRight()\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen():\n",
    "            street.moveRight()\n",
    "        cnt = delta = 0\n",
    "        street.moveRight()\n",
    "        while delta <= k:\n",
    "            while delta <= k and not street.isDoorOpen():\n",
    "                delta += 1\n",
    "                street.moveRight()\n",
    "            street.closeDoor()\n",
    "            delta += 1\n",
    "            street.moveRight()\n",
    "            if delta <= k:\n",
    "                cnt += delta\n",
    "                delta = 0\n",
    "        return cnt \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen():\n",
    "            street.moveRight()\n",
    "        for i in range(1, k + 1):\n",
    "            street.moveRight()\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                ans = i\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen(): street.moveRight()\n",
    "        street.moveRight()\n",
    "        res, last = 0, 0\n",
    "        for i in range(k):\n",
    "            last += 1\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                res += last\n",
    "                last = 0\n",
    "            street.moveRight()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# # Definition for a street.\n",
    "# # class Street:\n",
    "# #     def closeDoor(self):\n",
    "# #         pass\n",
    "# #     def isDoorOpen(self):\n",
    "# #         pass\n",
    "# #     def moveRight(self):\n",
    "# #         pass\n",
    "# class Solution:\n",
    "#     def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "#         while not street.isDoorOpen():\n",
    "#             street.moveRight()\n",
    "#         cnt = delta = 0\n",
    "#         street.moveRight()\n",
    "#         while delta <= k:\n",
    "#             while delta <= k and not street.isDoorOpen():\n",
    "#                 delta += 1\n",
    "#                 street.moveRight()\n",
    "#             street.closeDoor()\n",
    "#             delta += 1\n",
    "#             street.moveRight()\n",
    "#             if delta <= k:\n",
    "#                 cnt += delta\n",
    "#                 delta = 0\n",
    "#         return cnt \n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def houseCount(self, s: Optional['Street'], k: int) -> int:\n",
    "\n",
    "        while not s.isDoorOpen():\n",
    "\n",
    "            s.moveRight()\n",
    "\n",
    "        for i in range(1, k + 1):\n",
    "\n",
    "            s.moveRight()\n",
    "\n",
    "            if s.isDoorOpen():\n",
    "\n",
    "                ans = i\n",
    "\n",
    "                s.closeDoor()\n",
    "\n",
    "        return ans\n",
    "\n",
    "# 作者：灵茶山艾府\n",
    "# 链接：https://leetcode.cn/problems/count-houses-in-a-circular-street-ii/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen():\n",
    "            street.moveRight()\n",
    "        street.moveRight()\n",
    "        dist = 0\n",
    "        record = 0\n",
    "        empty = 0\n",
    "        while True:\n",
    "            dist += 1\n",
    "            if street.isDoorOpen():\n",
    "                record = dist\n",
    "                empty = 0\n",
    "                street.closeDoor()\n",
    "            else:\n",
    "                empty += 1\n",
    "                if empty > k:\n",
    "                    return record\n",
    "            street.moveRight()\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen() :\n",
    "            street.moveRight()\n",
    "        ans = 0\n",
    "        ret = 0\n",
    "        for i in range(k) :\n",
    "            street.moveRight()\n",
    "            ret += 1\n",
    "            if street.isDoorOpen() :\n",
    "                ans = ret \n",
    "                street.closeDoor()\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def houseCount(self, s: Optional['Street'], k: int) -> int:\n",
    "        while not s.isDoorOpen():\n",
    "            s.moveRight()\n",
    "        for i in range(1, k + 1):\n",
    "            s.moveRight()\n",
    "            if s.isDoorOpen():\n",
    "                ans = i\n",
    "                s.closeDoor()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen():\n",
    "            street.moveRight()\n",
    "        street.moveRight()\n",
    "        res=0\n",
    "        t=1\n",
    "        while t<=k:\n",
    "            if street.isDoorOpen():\n",
    "                res+=t\n",
    "                t=0\n",
    "                street.closeDoor()\n",
    "            street.moveRight()\n",
    "            t+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def houseCount(self, s: Optional['Street'], k: int) -> int:\n",
    "        while not s.isDoorOpen():\n",
    "            s.moveRight()\n",
    "        for i in range(1, k + 1):\n",
    "            s.moveRight()\n",
    "            if s.isDoorOpen():\n",
    "                ans = i\n",
    "                s.closeDoor()\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen():\n",
    "            street.moveRight()\n",
    "        result = 0\n",
    "        for i in range(1, k+1):\n",
    "            street.moveRight()\n",
    "            if street.isDoorOpen():\n",
    "                result = i\n",
    "                street.closeDoor()\n",
    "        return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, s: Optional['Street'], k: int) -> int:\n",
    "        while not s.isDoorOpen():\n",
    "            s.moveRight()\n",
    "        for i in range(1, k + 1):\n",
    "            s.moveRight()\n",
    "            if s.isDoorOpen():\n",
    "                ans = i\n",
    "                s.closeDoor()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen(): street.moveRight()\n",
    "        street.moveRight()\n",
    "        last = 0\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen(): street.moveRight()\n",
    "        street.moveRight()\n",
    "        last = 0\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, s: Optional['Street'], k: int) -> int:\n",
    "        while not s.isDoorOpen():\n",
    "            s.moveRight()\n",
    "        res = 0\n",
    "        for i in range(1,k+1):\n",
    "            s.moveRight()\n",
    "            if s.isDoorOpen():\n",
    "                s.closeDoor()\n",
    "                res = i\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while not street.isDoorOpen(): street.moveRight()\n",
    "        street.moveRight()\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        for _ in range(k):\n",
    "            street.openDoor()\n",
    "            street.moveLeft()\n",
    "        \n",
    "        ans = 0\n",
    "        while street.isDoorOpen():\n",
    "            ans += 1\n",
    "            street.closeDoor()\n",
    "            street.moveLeft()\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        for i in range(k):\n",
    "            street.closeDoor()\n",
    "            street.moveRight()\n",
    "        ans = 0\n",
    "        while not street.isDoorOpen():\n",
    "            street.openDoor()\n",
    "            street.moveRight()\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        street.moveLeft()\n",
    "        street.openDoor()\n",
    "        cnt = ans = 0\n",
    "        while cnt < k:\n",
    "            street.moveRight()\n",
    "            cnt += 1\n",
    "            if street.isDoorOpen():\n",
    "                ans = cnt\n",
    "            street.closeDoor()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        street.openDoor()\n",
    "        street.moveRight()\n",
    "        last = 0\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "      for i in range(k):\n",
    "        street.openDoor()\n",
    "        street.moveLeft()\n",
    "      res=0\n",
    "      while street.isDoorOpen():\n",
    "        res+=1\n",
    "        street.closeDoor()\n",
    "        street.moveLeft()\n",
    "      return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        cnt = k\n",
    "        while(cnt):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "            street.moveLeft()\n",
    "            cnt -= 1\n",
    "        res = 1\n",
    "        street.openDoor()\n",
    "        street.moveLeft()\n",
    "        while(not street.isDoorOpen()):\n",
    "            res += 1\n",
    "            street.moveLeft()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "\n",
    "        # 先打开所有的门\n",
    "        for _ in range(k):\n",
    "            street.openDoor()\n",
    "            street.moveRight()\n",
    "\n",
    "        # 关闭当前门做标记\n",
    "        num = 1\n",
    "        street.closeDoor()\n",
    "        street.moveRight()\n",
    "        while street.isDoorOpen():\n",
    "            num += 1\n",
    "            street.moveRight()\n",
    "        \n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    # 两次循环\n",
    "    # def houseCount1(self, street: Optional['Street'], k: int) -> int:\n",
    "    #     counter = 1\n",
    "    #     while counter <= k:\n",
    "    #         if street.isDoorOpen() == False:\n",
    "    #             street.openDoor()\n",
    "    #         street.moveLeft()\n",
    "    #         counter += 1\n",
    "        \n",
    "    #     res = 0\n",
    "    #     while street.isDoorOpen() == True:\n",
    "    #         res += 1\n",
    "    #         street.closeDoor()\n",
    "    #         street.moveLeft()\n",
    "\n",
    "    #     return res\n",
    "\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        # lastdoor\n",
    "        street.moveRight()\n",
    "        if street.isDoorOpen() == False:\n",
    "            street.openDoor()\n",
    "        \n",
    "        counter, res = 0, 0\n",
    "        while counter < k:\n",
    "            street.moveLeft()\n",
    "            counter += 1\n",
    "            if street.isDoorOpen() == True:\n",
    "                street.closeDoor()\n",
    "                res = counter\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        while k:\n",
    "            street.closeDoor()\n",
    "            street.moveRight()\n",
    "            k -= 1\n",
    "        street.openDoor()\n",
    "        street.moveRight()\n",
    "        ans = 1\n",
    "        while not street.isDoorOpen():\n",
    "            ans += 1\n",
    "            street.moveRight()\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        for _ in range(k):\n",
    "            street.closeDoor()\n",
    "            street.moveRight()\n",
    "        cnt=0\n",
    "        while not street.isDoorOpen():\n",
    "            cnt+=1\n",
    "            street.openDoor()\n",
    "            street.moveRight()\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        for _ in range(k):\n",
    "            street.closeDoor()\n",
    "            street.moveRight()\n",
    "        cnt=0\n",
    "        while not street.isDoorOpen():\n",
    "            cnt+=1\n",
    "            street.openDoor()\n",
    "            street.moveRight()\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        ans = 0\n",
    "        for _ in range(k):\n",
    "            street.closeDoor()\n",
    "            street.moveRight()\n",
    "        while 1:\n",
    "            if street.isDoorOpen():\n",
    "                break \n",
    "            street.openDoor()\n",
    "            street.moveRight()\n",
    "            ans += 1        \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        street.openDoor()\n",
    "        street.moveRight()\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        street.openDoor()\n",
    "        street.moveRight()\n",
    "        for i in range(k):\n",
    "            if street.isDoorOpen():\n",
    "                street.closeDoor()\n",
    "                last = i\n",
    "            street.moveRight()\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        for i in range(k):\n",
    "           street.closeDoor()\n",
    "           street.moveRight()\n",
    "        street.openDoor()\n",
    "        r = 1\n",
    "        street.moveRight()\n",
    "        while not street.isDoorOpen():\n",
    "            r += 1\n",
    "            street.moveRight()\n",
    "        return r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        result = 0\n",
    "        for _ in range(k):\n",
    "            street.isDoorOpen() and street.closeDoor() or street.moveRight()\n",
    "        while not street.isDoorOpen():\n",
    "            street.openDoor() or street.moveRight()\n",
    "            result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# Definition for a street.\n",
    "# class Street:\n",
    "#     def openDoor(self):\n",
    "#         pass\n",
    "#     def closeDoor(self):\n",
    "#         pass\n",
    "#     def isDoorOpen(self):\n",
    "#         pass\n",
    "#     def moveRight(self):\n",
    "#         pass\n",
    "#     def moveLeft(self):\n",
    "#         pass\n",
    "class Solution:\n",
    "    def houseCount(self, street: Optional['Street'], k: int) -> int:\n",
    "        counter = 1\n",
    "        while counter <= k:\n",
    "            if street.isDoorOpen() == False:\n",
    "                street.openDoor()\n",
    "            street.moveLeft()\n",
    "            counter += 1\n",
    "        \n",
    "        res = 0\n",
    "        while street.isDoorOpen() == True:\n",
    "            res += 1\n",
    "            street.closeDoor()\n",
    "            street.moveLeft()\n",
    "\n",
    "        return res\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
